package com.itjin.controller;

import com.itjin.model.domain.dto.ActivityCreateDTO;
import com.itjin.model.domain.dto.ActivityEditDTO;
import com.itjin.model.domain.dto.ActivityJoinDTO;
import com.itjin.model.domain.vo.ActivityDetailVO;
import com.itjin.model.domain.vo.ActivityListVO;
import com.itjin.model.result.PageResult;
import com.itjin.model.result.Result;
import com.itjin.service.CampusActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 校园活动控制器
 */
@Tag(name = "校园活动管理", description = "校园活动相关功能接口")
@RestController
@RequestMapping("/api/v1/activity-planners")
@Slf4j
public class CampusActivityController {

    @Autowired
    private CampusActivityService campusActivityService;

    /**
     * 创建活动
     */
    @PostMapping("/create")
    @Operation(summary = "创建活动")
    public Result<ActivityDetailVO> createActivity(@Valid @RequestBody ActivityCreateDTO activityCreateDTO) {
        try {
            log.info("创建活动请求，举办人ID：{}，活动名称：{}", activityCreateDTO.getOrganizerId(), activityCreateDTO.getActivityName());
            
            ActivityDetailVO result = campusActivityService.createActivity(activityCreateDTO);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("创建活动失败：", e);
            return Result.failure(500, "创建活动失败：" + e.getMessage());
        }
    }

    /**
     * 编辑活动
     */
    @PutMapping("/edit")
    @Operation(summary = "编辑活动")
    public Result<ActivityDetailVO> editActivity(@Valid @RequestBody ActivityEditDTO activityEditDTO) {
        try {
            log.info("编辑活动请求，活动ID：{}", activityEditDTO.getId());
            
            ActivityDetailVO result = campusActivityService.editActivity(activityEditDTO);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("编辑活动失败：", e);
            return Result.failure(500, "编辑活动失败：" + e.getMessage());
        }
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{activityId}")
    @Operation(summary = "删除活动")
    public Result<Boolean> deleteActivity(
            @Parameter(description = "活动ID", required = true) @PathVariable("activityId") Integer activityId,
            @Parameter(description = "操作者ID", required = true) @RequestParam("operatorId") Integer operatorId) {
        
        try {
            log.info("删除活动请求，活动ID：{}，操作者ID：{}", activityId, operatorId);
            
            boolean result = campusActivityService.deleteActivity(activityId, operatorId);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("删除活动失败：", e);
            return Result.failure(500, "删除活动失败：" + e.getMessage());
        }
    }

    /**
     * 获取活动详情
     */
    @GetMapping("/{activityId}")
    @Operation(summary = "获取活动详情")
    public Result<ActivityDetailVO> getActivityDetail(
            @Parameter(description = "活动ID", required = true) @PathVariable("activityId") Integer activityId,
            @Parameter(description = "用户ID") @RequestParam(value = "userId", required = false) Integer userId) {
        
        try {
            log.info("获取活动详情请求，活动ID：{}，用户ID：{}", activityId, userId);
            
            ActivityDetailVO result = campusActivityService.getActivityDetail(activityId, userId);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("获取活动详情失败：", e);
            return Result.failure(500, "获取活动详情失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询活动列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询活动列表")
    public Result<PageResult<ActivityListVO>> getActivityList(
            @Parameter(description = "活动类型") @RequestParam(value = "activityType", required = false) Integer activityType,
            @Parameter(description = "活动状态") @RequestParam(value = "activityStatus", required = false) Integer activityStatus,
            @Parameter(description = "当前页", required = true) @RequestParam("current") Long current,
            @Parameter(description = "每页大小", required = true) @RequestParam("size") Long size) {
        
        try {
            log.info("分页查询活动列表，类型：{}，状态：{}，页码：{}，大小：{}", activityType, activityStatus, current, size);
            
            PageResult<ActivityListVO> result = campusActivityService.getActivityList(activityType, activityStatus, current, size);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("分页查询活动列表失败：", e);
            return Result.failure(500, "查询活动列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取我创建的活动列表
     */
    @GetMapping("/my-created")
    @Operation(summary = "获取我创建的活动列表")
    public Result<List<ActivityListVO>> getMyCreatedActivities(
            @Parameter(description = "举办人ID", required = true) @RequestParam("organizerId") Integer organizerId) {
        
        try {
            log.info("获取我创建的活动列表，举办人ID：{}", organizerId);
            
            List<ActivityListVO> result = campusActivityService.getMyCreatedActivities(organizerId);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("获取我创建的活动列表失败：", e);
            return Result.failure(500, "获取我创建的活动列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取我参与的活动列表
     */
    @GetMapping("/my-joined")
    @Operation(summary = "获取我参与的活动列表")
    public Result<List<ActivityListVO>> getMyJoinedActivities(
            @Parameter(description = "用户ID", required = true) @RequestParam("userId") Integer userId) {
        
        try {
            log.info("获取我参与的活动列表，用户ID：{}", userId);
            
            List<ActivityListVO> result = campusActivityService.getMyJoinedActivities(userId);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("获取我参与的活动列表失败：", e);
            return Result.failure(500, "获取我参与的活动列表失败：" + e.getMessage());
        }
    }

    /**
     * 参加活动
     */
    @PostMapping("/join")
    @Operation(summary = "参加活动")
    public Result<Boolean> joinActivity(@Valid @RequestBody ActivityJoinDTO activityJoinDTO) {
        try {
            log.info("参加活动请求，活动ID：{}，用户ID：{}", activityJoinDTO.getActivityId(), activityJoinDTO.getUserId());
            
            boolean result = campusActivityService.joinActivity(activityJoinDTO);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("参加活动失败：", e);
            return Result.failure(500, "参加活动失败：" + e.getMessage());
        }
    }

    /**
     * 退出活动
     */
    @PostMapping("/quit")
    @Operation(summary = "退出活动")
    public Result<Boolean> quitActivity(
            @Parameter(description = "活动ID", required = true) @RequestParam("activityId") Integer activityId,
            @Parameter(description = "用户ID", required = true) @RequestParam("userId") Integer userId) {
        
        try {
            log.info("退出活动请求，活动ID：{}，用户ID：{}", activityId, userId);
            
            boolean result = campusActivityService.quitActivity(activityId, userId);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("退出活动失败：", e);
            return Result.failure(500, "退出活动失败：" + e.getMessage());
        }
    }

    /**
     * 获取热门活动列表
     */
    @GetMapping("/popular")
    @Operation(summary = "获取热门活动列表")
    public Result<List<ActivityListVO>> getPopularActivities(
            @Parameter(description = "限制数量") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        
        try {
            log.info("获取热门活动列表，限制数量：{}", limit);
            
            List<ActivityListVO> result = campusActivityService.getPopularActivities(limit);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("获取热门活动列表失败：", e);
            return Result.failure(500, "获取热门活动列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取即将开始的活动列表
     */
    @GetMapping("/upcoming")
    @Operation(summary = "获取即将开始的活动列表")
    public Result<List<ActivityListVO>> getUpcomingActivities() {
        try {
            log.info("获取即将开始的活动列表");
            
            List<ActivityListVO> result = campusActivityService.getUpcomingActivities();
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("获取即将开始的活动列表失败：", e);
            return Result.failure(500, "获取即将开始的活动列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据类型获取活动列表
     */
    @GetMapping("/by-type")
    @Operation(summary = "根据类型获取活动列表")
    public Result<List<ActivityListVO>> getActivitiesByType(
            @Parameter(description = "活动类型", required = true) @RequestParam("activityType") Integer activityType,
            @Parameter(description = "限制数量") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        
        try {
            log.info("根据类型获取活动列表，类型：{}，限制数量：{}", activityType, limit);
            
            List<ActivityListVO> result = campusActivityService.getActivitiesByType(activityType, limit);
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("根据类型获取活动列表失败：", e);
            return Result.failure(500, "根据类型获取活动列表失败：" + e.getMessage());
        }
    }
} 